﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Hotnet.Entity;
using Hotnet.Biz.NSBizPicture;
using Hotnet.Biz.NSBizCommon;
using Hotnet.Entity.Enum;
using System.Text;
using ZoomImageDemo;
using Component.Utilities;
using System.IO;

namespace Hotnet.Web.Controllers.Admin
{
	public class AdminPictureGroupController : AdminBaseController
	{
		//
		// GET: /AdminPictureGroup/

		private BizPictureGroup bizPictureGroup;
		private BizPictureGroup BizPictureGroup
		{
			get
			{
				if (bizPictureGroup == null)
				{
					bizPictureGroup = new BizPictureGroup();
				}
				return bizPictureGroup;
			}
		}
		private BizPicture bizPicture;
		private BizPicture BizPicture
		{
			get
			{
				if (bizPicture == null)
					bizPicture = new BizPicture();
				return bizPicture;
			}
		}
		private BizPictureCategory bizPictureCategory;
		private BizPictureCategory BizPictureCategory
		{
			get
			{
				if (bizPictureCategory == null)
					bizPictureCategory = new BizPictureCategory();
				return bizPictureCategory;
			}
		}
		private BizSourceFrom bizSourceFrom;
		private BizSourceFrom BizSourceFrom
		{
			get
			{
				if (bizSourceFrom == null)
				{
					bizSourceFrom = new BizSourceFrom();
				}
				return bizSourceFrom;
			}
		}
		private BizBigEye bizBigEye;
		private BizBigEye BizBigEye
		{
			get
			{
				if (bizBigEye == null)
				{
					bizBigEye = new BizBigEye();
				}
				return bizBigEye;
			}
		}
		private BizPictureGroupXTag bizPictureGroupXTag;
		private BizPictureGroupXTag BizPictureGroupXTag
		{
			get
			{
				if (bizPictureGroupXTag == null)
					bizPictureGroupXTag = new BizPictureGroupXTag();
				return bizPictureGroupXTag;
			}
		}

		private BizPictureList bizPictureList;
		private BizPictureList BizPictureList
		{
			get
			{
				if (bizPictureList == null)
					bizPictureList = new BizPictureList();
				return bizPictureList;
			}
		}

		public ActionResult Index(int id)
		{
			var pictureList = BizPicture.List(id);
			var group = BizPictureGroup.Get(id);
			ViewData["Category"] = BizPictureCategory.Get(group.CategoryID);
			ViewData["Group"] = group;
			SourceFrom[] sourceFrom = BizSourceFrom.List(EnumSystemCategory.图片);
			SelectList sourceFromList = new SelectList(sourceFrom, "SourceFromID", "Name", null);
			ViewData["SourceFromList"] = sourceFromList;
			return View(pictureList);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(PictureGroup group)
		{
			if(group.OrderInCategory == null)
				group.OrderInCategory = 0;
			group.IsPublished = (int)EnumPublishState.未发布;
			group.DefineType = (int)EnumDefineType.None;
			group.AddTime = DateTime.Now;
			BizPictureGroup.Insert(group);
			return RedirectToAction("Index", "AdminPictureCategory", new { id = group.CategoryID });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(PictureGroup group)
		{
			BizPictureGroup.UpdateAll(group);
			return RedirectToAction("Index", "AdminPictureCategory", new { id = group.CategoryID });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InnerEdit(PictureGroup group)
		{
			BizPictureGroup.UpdateAll(group);
			return RedirectToAction("Index", new { id = group.GroupID });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(int id)
		{
			var group = BizPictureGroup.Get(id);
			BizPictureGroup.Delete(id);
			return RedirectToAction("Index", "AdminPictureCategory", new { id = group.CategoryID });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public void SetBigEye(int groupId)
		{
			BigEye bigEye = new BigEye();
			bigEye.TargetEyeID = groupId;
			bigEye.TargetType = (int)EnumSystemCategory.图片;
			bigEye.OrderNo = 0;
			BizBigEye.Insert(bigEye);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public void UnsetBigEye(int groupId)
		{
			BizBigEye.DeleteByGroupIDAndCategoryType(groupId, (int)EnumSystemCategory.图片);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public void SetPublished(int groupId)
		{
			BizPictureGroup.Update(groupId,x=> new PictureGroup() { IsPublished = 1 });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public void UnSetPublished(int groupId)
		{
			BizPictureGroup.Update(groupId, x => new PictureGroup() { IsPublished = 0 });
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public void SetCommend(int groupId)
		{
			BizPictureGroup.Update(
				group => group.GroupID == groupId,
				group => new PictureGroup { DefineType = (int)EnumDefineType.我的推荐 }
			);
		}


		[AcceptVerbs(HttpVerbs.Post)]
		public void UnsetCommend(int groupId)
		{
			BizPictureGroup.Update(
				group => group.GroupID == groupId , 
				group => new PictureGroup { DefineType = (int)EnumDefineType.None }
			);
		}

		public ActionResult Get(int id)
		{
			var group = BizPictureGroup.GetAll(id);
			return Json(group);
		}

		/// <summary>
		/// 设置图片列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult EditPictureList(int id,EnumPictureListType? listType,string retUrl)
		{
			PictureGroup pictureGroup = BizPictureGroup.Get(id);
			Picture[] pictures = BizPicture.List(id);
			ViewData["PictureGroup"] = pictureGroup;
			ViewData["Pictures"] = pictures;
			ViewData["listType"] = listType ?? 0;
			ViewData["RetUrl"] = retUrl;

			PictureList[] pictureList = BizPictureList.List(id);
			ViewData["PictureList"] = pictureList;
			return View();
		}

		public ActionResult EditSinglePictureList(int id,EnumPictureListType listType)
		{
			ViewData["Picture"] = BizPicture.Get(id);
			ViewData["ListType"] = listType;
			switch (listType)
			{
				case EnumPictureListType.首页焦点图片:
					ViewData["CANVAS_WIDTH"] = 486;
					ViewData["CANVAS_HEIGHT"] = 602;
					ViewData["ICON_WIDTH"] = 375;
					ViewData["ICON_HEIGHT"] = 440;
					break;
				case EnumPictureListType.分类焦点图片:
					ViewData["CANVAS_WIDTH"] = 702;
					ViewData["CANVAS_HEIGHT"] = 602;
					ViewData["ICON_WIDTH"] = 598;
					ViewData["ICON_HEIGHT"] = 456;
					break;
				default:
					break;
			}


			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult SetRecommendPictureList(int id)
		{
			PictureList plist = new PictureList();
			plist.ListType = (int)EnumPictureListType.推荐图片;
			plist.GroupID = BizPicture.Get(id).GroupID;
			plist.AddTime = DateTime.Now;
			plist.IsPublished = (int)EnumPublishState.发布;

			plist.CoverImageSrc = BizPicture.Get(id).ThumbnailPath;
			BizPictureList.Delete(plist.GroupID, EnumPictureListType.推荐图片);
			BizPictureList.Insert(plist);
			return Content("设置推荐列表成功");
		}

		[AcceptVerbs( HttpVerbs.Post)]
		public ActionResult EditSinglePictureList(int id,EnumPictureListType listType,string txt_width, string txt_height, string txt_top, string txt_left, string txt_DropWidth, string txt_DropHeight)
		{
			int imageWidth = Int32.Parse(txt_width);
			int imageHeight = Int32.Parse(txt_height);
			int cutTop = Int32.Parse(txt_top);
			int cutLeft = (int)double.Parse(txt_left);
			int dropWidth = Int32.Parse(txt_DropWidth);
			int dropHeight = Int32.Parse(txt_DropHeight);
			Picture image = BizPicture.Get(id);
			string folder = "";
			switch (listType)
			{
				case EnumPictureListType.首页焦点图片:
					folder = Biz.Properties.Settings.Default.首页焦点图片文件夹;
					break;
				case EnumPictureListType.分类焦点图片:
					folder = Biz.Properties.Settings.Default.分类焦点图片文件夹;
					break;
				default:
					break;
			}
			PictureList pl = new PictureList();
			pl.GroupID = image.GroupID;
			pl.ListType = (int)listType;
			pl.AddTime = DateTime.Now;
			pl.IsPublished = (int)EnumPublishState.发布;
			if (listType != EnumPictureListType.推荐图片)
			{
				string imageName = null;
				string dir = StringUtility.GetSavePath(id, out imageName);
				string LocalPath = dir + imageName + "_o" + Path.GetExtension(image.LocalPath);

				string filename = CutPhotoHelp.SaveCutPic(Server.MapPath(Biz.Properties.Settings.Default.UploadFolder + image.LocalPath), Server.MapPath(folder+LocalPath), 0, 0, dropWidth,
										dropHeight, cutLeft, cutTop, imageWidth, imageHeight);
				pl.CoverImageSrc = LocalPath;
			}
			BizPictureList.Delete(pl.GroupID,listType);
			BizPictureList.Insert(pl);

			return RedirectToAction("EditPictureList", new { id=image.GroupID,listType=(int)listType });
		}

		public static string GetPictureListImageSrc(PictureList pictureList)
		{
			EnumPictureListType listType = (EnumPictureListType)pictureList.ListType;
			switch (listType)
			{
				case EnumPictureListType.推荐图片:
					return Biz.Properties.Settings.Default.UploadFolder+pictureList.CoverImageSrc;
				case EnumPictureListType.首页焦点图片:
					return Biz.Properties.Settings.Default.首页焦点图片文件夹 + pictureList.CoverImageSrc;
				case EnumPictureListType.分类焦点图片:
					return Biz.Properties.Settings.Default.分类焦点图片文件夹 + pictureList.CoverImageSrc; ;
				default:
					break;
			}
			return "";
		}

	}
}